package com.acme.bankapp.service.bank;

import java.io.IOException;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.BankException;
import com.acme.bankapp.domain.bank.NotEnoughFundsException;
import com.acme.bankapp.domain.bank.OverDraftLimitExceededException;
import com.acme.bankapp.service.bank.BankDataLoaderService.FeedLoadException;
import com.acme.bankapp.service.bank.BankService.runCase;

public class BankApplication 
{
	
	public static void main(String[] args) 
	{
		try {
			
			Bank bank = null;
			switch (BankService.getCase(args)) {
			case CASE_FEED:
				bank = BankService.loadFromFeedBank(args[1]);
				break;
			case CASE_DEFAULT:
				bank = BankService.createDefaultBank();
				break;
			case CASE_SERVER:
				bank = BankService.serializeBank(args[1]);
				BankService.startServer(bank);
				return;
			case CASE_SERIALIZE:
				bank = BankService.serializeBank(args[1]);
				break;
			default:
				bank = BankService.createEmptyBank();
			}
			
			/* simple operations with bank */
			BankService.printMaximumAmountToWithdraw(bank);
			try {
				BankService.modifyBank(bank);
			}
			catch (BankException e) {
				 if (e instanceof OverDraftLimitExceededException) {
						System.out.println("Overdraft limit exceeded exception");
						OverDraftLimitExceededException o = (OverDraftLimitExceededException)e;
						System.out.println("Maximum amount to withdraw: " + o.getMaximumAmountToWithdraw());
						System.out.println("Trying to withdraw: " + (o.getMaximumAmountToWithdraw() + o.getAmount()));
						System.out.println("Limit exceeded by: " + o.getAmount());
					}
				else if (e instanceof NotEnoughFundsException) {
					System.out.println("Not enough funds exception");
				}
			}
			BankService.printMaximumAmountToWithdraw(bank);
			//BankService.printClients(bank);
			/* */
			
		} catch (IOException e) {
			System.out.println("I/O error");
		} catch (FeedLoadException e) {
			System.out.println("Unable to load bank from feed file");
		}
	}
	
}
